package mines.eerock.sphero.bowling.audiobutton;

import mines.eerock.sphero.bowling.util.Params;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;

public class MediaButtonBlocker extends BroadcastReceiver {


	private static final ComponentName BLOCKER = new ComponentName("mines.eerock.sphero.bowling",
			"mines.eerock.sphero.bowling.audiobutton.MediaButtonBlocker");

	@Override
	public void onReceive(Context context, Intent intent) {
		if (Params.LOGGING_ENABLED) {
			Log.i(Params.LOGGING_TAG, "Blocking MEDIA_BUTTON action.");
		}

		// Abort the broadcast.  No one will ever believe you.
		abortBroadcast();
	}

	/**
	 * Returns if the App is currently blocking Intents from AudioJack
	 * @param context
	 * @return
	 */
	public static boolean isBlocking(Context context) {
		PackageManager pm = context.getPackageManager();
		return pm.getComponentEnabledSetting(BLOCKER) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
	}
	
	/**
	 * Sets Blocking to be true or false to block false AudioJack Intents
	 * @param context
	 * @param setValue
	 */
	public static void setBlocking(Context context, boolean setValue) {
		if (Params.LOGGING_ENABLED) {
			Log.i(Params.LOGGING_TAG, "Set Blocking to " + new Boolean(setValue).toString());
		}
		
		PackageManager pm = context.getPackageManager();
		int newState = (setValue) ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
				: PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
		pm.setComponentEnabledSetting(BLOCKER, newState, PackageManager.DONT_KILL_APP);
	}
}
